package javaTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ASavetheMagazinesOther {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        int l = Integer.parseInt(br.readLine());
        while (l-- > 0) {
            solve();
        }
        br.close();
    }

    public static void solve() throws IOException {
        int n = Integer.parseInt(br.readLine());
        String str = br.readLine();
        int[] GaiZi = new int[n+1];
        for (int i = 1; i <= n; i++) {
            GaiZi[i] = str.charAt(i-1) - '0';
        }
        int[] a = new int[n+1];
        String numberStr = br.readLine();
        String[] numbers = numberStr.split(" ");
        for (int i = 1; i <= n; i++) {
            a[i] = Integer.parseInt(numbers[i-1]);
        }
        long[][] dp = new long[n+1][2];
        dp[0][0] = dp[0][1] = 0l;
        for(int i = 1; i<=n; i++){
            if(GaiZi[i] == 1){
                dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]) + a[i];
                dp[i][0] = dp[i-1][0] + a[i-1];
            }else{
                dp[i][1] = dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]);
            }
        }
        System.out.println(Math.max(dp[n][0],dp[n][1]));
    }
}
